package com.lj.queue.handler;

import com.lj.queue.service.DelayQueueMessageService;
import com.lj.queue.service.DelayQueueMetaService;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.concurrent.DelayQueue;

/**
 * @author luojing
 * @since 2025/7/17 10:41
 */
@Data
public class DelayHandlerContext {

    /**
     * queueMeta的id
     */
    private Long queueId;

    /**
     * 队列名称
     */
    private String queueName;

    /**
     * 当前队列实例
     */
    private DelayQueue<DelayedMessage> delayQueue;

    /**
     * 消息id
     */
    private Long messageId;

    /**
     * 消息内容
     */
    private String message;

    /**
     * 消息期望执行时间
     */
    private LocalDateTime expectTime;

    /**
     * 延迟队列信息Service
     * 如果延迟消息处理器注入这个类的话，会有循环依赖的问题,所以直接在上下文上拿
     */
    private DelayQueueMetaService delayQueueMetaService;

    /**
     * 信息Service
     */
    private DelayQueueMessageService queueMessageService;

    public static DelayHandlerContext of(String queueName, DelayedMessage delayedMessage, DelayQueue<DelayedMessage> delayQueue,
                                         DelayQueueMetaService delayQueueMetaService, DelayQueueMessageService queueMessageService) {
        DelayHandlerContext context = new DelayHandlerContext();
        context.queueId = delayedMessage.getQueueId();
        context.queueName = queueName;
        context.delayQueue = delayQueue;
        context.messageId = delayedMessage.getMessageId();
        context.message = delayedMessage.getMessage();
        context.expectTime = delayedMessage.getExpectTime();
        context.delayQueueMetaService = delayQueueMetaService;
        context.queueMessageService = queueMessageService;
        return context;
    }

}
